export class  Math2D
{
    public static dot(v1:IFVector2,v2:IFVector2)
    {
        return v1.x*v2.x+v1.y*v2.y;
    }

    public static  cross(v1:IFVector2,v2:IFVector2)
    {
        return v1.x*v2.y-v2.x*v1.y;
    }

    public  static angle(v1:IFVector2,v2:IFVector2)
    {
        const v1_len = Math.sqrt(v1.x*v1.x+v1.y*v1.y);
        const v2_len = Math.sqrt(v2.x*v2.x+v2.y*v2.y);
        const cos_o =  Math2D.dot(v1,v2)/(v1_len*v2_len);
        const angle =  Math.acos(cos_o);
        return angle;
    }
    public  static radian(v1:IFVector2,v2:IFVector2)
    {
        const v1_len = Math.sqrt(v1.x*v1.x+v1.y*v1.y);
        const v2_len = Math.sqrt(v2.x*v2.x+v2.y*v2.y);
        const cos_o =  Math2D.dot(v1,v2)/(v1_len*v2_len);
        const angle =  Math.acos(cos_o);
        if(Math2D.cross(v2,v1)>=0)
        {
            return  angle;
        }
        else
        {
            return Math.PI*2- angle ;
        }
    }
    public  static angleZ(v1:IFVector2,v2:IFVector2)
    {
        const v1_len = Math.sqrt(v1.x*v1.x+v1.y*v1.y);
        const v2_len = Math.sqrt(v2.x*v2.x+v2.y*v2.y);
        const cos_o =  Math2D.dot(v1,v2)/(v1_len*v2_len);
        const angle =  Math.acos(cos_o);
        if(Math2D.cross(v2,v1)>0)
        {
            return  Math.round(angle*180/Math.PI);
        }
        else
        {
            return Math.round((Math.PI*2- angle)*180/Math.PI) ;
        }

    }
    public static add(v1:IFVector2,v2:IFVector2)
    {
        return {x:v1.x+v2.x,y:v1.y+v2.y}
    }
    public static subtract(v1:IFVector2,v2:IFVector2)
    {
        return {x:v1.x-v2.x,y:v1.y-v2.y}
    }
    public static multiply(v1:IFVector2,val:number)
    {
        return {x:v1.x*val,y:v1.y*val}
    }
    public static distance(v1:IFVector2)
    {
       return  Math.sqrt(v1.x*v1.x+v1.y*v1.y);
    }
    public static distanceto(v1:IFVector2,v2:IFVector2)
    {
       return  Math2D.distance({x:v1.x-v2.x,y:v1.y-v2.y});
    } 
    public static distanceMhd(v1:IFVector2,v2:IFVector2)
    {
        // v1 到v2 的曼哈顿距离
        
       return  Math.max(Math.abs(v1.x-v2.x),Math.abs(v1.y-v2.y));
    }
    public static distancetosqrt(v1:IFVector2,v2:IFVector2)
    {
       return  Math2D.distance({x:v1.x-v2.x,y:v1.y-v2.y});
    }
}
